<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小同住人员物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\room;

use think\App;
use app\controller\property\Base;
use app\validate\room\RoomBasicValidate;
use app\common\repositories\room\RoomBasicRepository;
use app\common\repositories\energy\EnergyMeterRepository;


class RoomBasic extends Base
{
    protected $repository;

    public function __construct(App $app, RoomBasicRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;

    }

    /**
     *  获取房间列表
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 23:31
     */
    public function roomList()
    {
        $where = $this->request->param([
            'cate_id' => '',
            'property_id' => '',
            'keywords' => ''
        ]);
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiRoomList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

    /**
     * 获取空置房间
     * @return mixed
     *
     * @author Amos
     * 2024-06-03 19:04
     */
    public function vacancyList()
    {
        $where = $this->request->param([
            'cate_id' => '',
            'label_id' => '',
            'property_id' => '',
            'keywords' => ''
        ]);
        $where['status'] = ['1,3,4'];
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiVacancyiList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

    /**
     * 添加房间
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-06-13 15:33
     */
    public function addRoom()
    {
        $param = $this->request->param([
            'property_id' => '',
            'unit_id' => '',
            'floor_name' => '',
            'floor_total' => '',
            'room_name' => '',
            'room_unit' => '',
            'room_num' => '',
            'hall_num' => '',
            'who_num' => '',
            'room_size' => '',
            'room_rent' => '',
            'room_deposit' => '',
            'direction' => '',
            'label_id' => '',
            'remark' => '',
            'picture' => ''
        ]);
        validate(RoomBasicValidate::class)->scene('add')->check($param);
        if ($this->repository->roomNameExists($param['property_id'], $param['room_name'], (int)$param['unit_id'])) {
            return $this->error('房间' . $param['room_name'] . '已存在');
        }
        $param['add_uid'] = $this->request->userId();
        if (!is_array($param['label_id'])) {
            $param['label_id'] = array_filter(explode(',', $param['label_id']));
        }
        $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId());
        return app('api_return')->success($res['id']);
    }

    /**
     *  编辑房间
     * @param int $id
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-13 15:32
     */
    public function editRoom(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $param = $this->request->param([
            'floor_name' => '',
            'floor_total' => '',
            'room_name' => '',
            'room_unit' => '',
            'room_num' => '',
            'hall_num' => '',
            'who_num' => '',
            'room_size' => '',
            'room_rent' => '',
            'label_id' => '',
            'room_deposit' => '',
            'direction' => '',
            'remark' => '',
            'picture' => ''
        ]);
        validate(RoomBasicValidate::class)->scene('add')->check($param);
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->repository->roomNameExists($info['property_id'], $param['room_name'], (int)$info['unit_id'], $info['id'])) {
            return $this->error('房间' . $param['room_name'] . '已存在');
        }
        if (!is_array($param['label_id'])) {
            $param['label_id'] = array_filter(explode(',', $param['label_id']));
        }
        $res = $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success($res);
    }


    /**
     *  删除房间
     * @param int $id
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-13 14:44
     */
    public function delete(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        ##  检测出租中的房间
        $res = $this->repository->deleteCheck($info);
        if ($res !== true) {
            return $this->error($res);
        }
        if ($info['add_uid'] != $this->request->userId()) {
            return app('api_return')->error('非法操作，非创建人禁止删除');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return app('api_return')->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

    /**
     *  获取房间详情
     * @param int $id
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-06-13 16:38
     */
    public function detail(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->getApiDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }


    /**
     * 绑定能源设备
     * @param int $id
     * @param EnergyMeterRepository $energyMeterRepository
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-13 15:42
     */
    public function bindEnergyMeter(int $id, EnergyMeterRepository $energyMeterRepository)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $param = $this->request->param([
            'meter_id' => '',
            'remark' => '',
        ]);
        $roomInfo = $this->repository->get($id);
        if (!$roomInfo) {
            return app('api_return')->error('数据不存在');
        }
        $param['property_id'] = $roomInfo['property_id'];
        $energyMeterRepository->bindRoom($param, $id, $this->request->userId());
        return app('api_return')->success('绑定成功');
    }

    /**
     *  设备解绑
     * @param EnergyMeterRepository $energyMeterRepository
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-13 16:03
     */
    public function unbindEnergyMeter(int $id, EnergyMeterRepository $energyMeterRepository)
    {
        if (!$energyMeterRepository->exists($id)) {
            return $this->error('数据不存在');
        }
        $remark = $this->request->param('remark');
        if ($remark == '') {
            return $this->error('请输入解绑原因');
        }
        $info = $energyMeterRepository->get($id);
        if ($info['id'] <= 0) {
            return $this->error('设备不存在');
        }
        $energyMeterRepository->unbindByInfo($info, $remark, $this->request->userId());
        return app('api_return')->success('解绑成功');
    }

    /**
     * 根据房音 ID 获取能源设备
     * @param int $id
     * @param EnergyMeterRepository $energyMeterRepository
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-13 16:36
     */
    public function energyMeterList(int $id, EnergyMeterRepository $energyMeterRepository)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $where = $this->request->param([
            'keywords' => '',
            'energy_type' => '',
        ]);
        $where['room_id'] = $id;
        [$page, $limit] = $this->getPage();
        $res = $energyMeterRepository->getRoomIdList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }


}